﻿using Hrm.Core;//
using Hrm.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;

namespace Hrm.Web.Controllers
{
    [RoleAuthorize]
    public class ClassMateMobileController : Controller
    {
        IClassMateService _classMateService;
        IUserService _userService;

        public ClassMateMobileController(IClassMateService classMateService, IUserService userService)
        {
            this._classMateService = classMateService;
            this._userService=userService;
        }
        public ActionResult Index()
        {
            PageRequest<ClassMateRequest> request = new PageRequest<ClassMateRequest>();
            //request.filter.Name = User.Identity.Name;
            request.start = 0;
            request.length = 10;
            request.filter = new ClassMateRequest();

            var result= this._classMateService.GetClassMatePage(request);
            @ViewBag.recordsTotal = result.recordsTotal;
            return View(result.data);
        }

        public JsonResult PageList(PageRequest<ClassMateRequest> request)
        {
            request.start = (request.start-1) * request.length;
            var result=this._classMateService.GetClassMatePage(request);
            return Json(result);
        }

        public ActionResult Self()
        {
            PageRequest<ClassMateRequest> request = new PageRequest<ClassMateRequest>();
            request.filter = new ClassMateRequest();
            request.filter.Name = User.Identity.Name;
            request.start = 0;
            request.length = 1;
            var result = this._classMateService.GetClassMatePage(request);
            return View(result.data.FirstOrDefault());
        }

        public ActionResult SelfEdit()
        {
            PageRequest<ClassMateRequest> request = new PageRequest<ClassMateRequest>();
            request.filter = new ClassMateRequest();
            request.filter.Name = User.Identity.Name;
            request.start = 0;
            request.length = 1;
            var result = this._classMateService.GetClassMatePage(request);
            return View(result.data.FirstOrDefault());
        }

        [HttpPost]
        public ActionResult SelfEdit(ClassMateDto model)
        {
            var userModel =this._classMateService.GetList(p=>p.Name==User.Identity.Name&&p.Id==model.Id).FirstOrDefault();
            if (TryUpdateModel(userModel, "", new string[] { "Name", "Birthday", "Sex","Address", "Phone", "QQ", "WeChat", "Company", "Description"}))
            {
                this._classMateService.Update(userModel);
            }
            return RedirectToAction("Self");
        }

        public ActionResult Setting()
        {

            return View();
        }

        public ActionResult ModifyPassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ModifyPassword(ModifyPasswordDto model)
        {
            if (model.OldPassword == model.NewPassword)
            {
                ModelState.AddModelError("NewPassword", "新旧密码不能相同！");
            }
            if (ModelState.IsValid)
            {
                var userCode = User.Identity.GetUserId();
                string oldPassword = EncryptionTool.getMd5(model.OldPassword);
                var user = this._userService.GetPerson(userCode);
                if (user != null && user.Password == oldPassword)
                {
                    user.Password = EncryptionTool.getMd5(model.NewPassword);
                    this._userService.Update(user);
                    return RedirectToAction("Index", "ClassMateMobile");
                }
                else
                {
                    ModelState.AddModelError("OldPassword", "原密码错误！");
                }
            }
            return View(model);
        }

        public ActionResult Search()
        {
            var list =new List<ClassMateDto>();
            return View(list);
        }

        [HttpPost]
        public ActionResult Search(string keyWord)
        {
            PageRequest<ClassMateRequest> request = new PageRequest<ClassMateRequest>();
            request.filter = new ClassMateRequest();
            request.filter.Name = keyWord;
            request.start = 0;
            request.length = 100;
            var result = this._classMateService.GetClassMatePage(request);
            return View(result.data);
        }

        [HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase upImg)
        {
            if (upImg != null)
            {
                string fileName = DateTime.Now.ToString("yyyyMMddhhmmssfff")+System.IO.Path.GetFileName(upImg.FileName);
                string fileSavePath = "../../Images/upload/" + fileName;
                string fileVirtualPath = "~/Images/upload/" + fileName;
                string filePhysicalPath = Server.MapPath(fileVirtualPath);
                string error = "";
                try
                {
                    upImg.SaveAs(filePhysicalPath);
                    var userModel = this._classMateService.GetList(p => p.Name == User.Identity.Name).FirstOrDefault();
                    userModel.ImageUrl = fileSavePath;
                    this._classMateService.Update(userModel);
                }
                catch (Exception ex)
                {
                    error = ex.Message;
                }
            }
            return RedirectToAction("SelfEdit");
        }

        public ActionResult About()
        {
            return View();
        }

        public FileResult ClassmateAddressBook()
        {
            var path = Server.MapPath("~/Files/ClassmateAddressBook.apk");
            return File(path, "application/apk", "ClassmateAddressBook.apk");
        }
    }
}